﻿using AIVision.MaoKe01.Application;
using AIVision.MaoKe01.Application.Event;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIVision.MaoKe01.ViewModels
{
    public class ViewMainViewModel : BindableBase
    {
        private readonly MachineService machineService;


        public DelegateCommand<string> ButtonCommand { get; private set; }
        public ObservableCollection<MessageEventArgs> Messages { get; set; } = new ObservableCollection<MessageEventArgs>();
        public MachineModel MachineModel { get; }

        public ViewMainViewModel(IEventAggregator eventAggregator,MachineService machineService, MachineModel machineModel)
        {
            eventAggregator.GetEvent<MessageEvent>().Subscribe(ReceiveMessageEvent, ThreadOption.UIThread);
            this.machineService = machineService;
            this.MachineModel = machineModel;
            ButtonCommand = new DelegateCommand<string>(ButtonMethod);

            Task.Run(async () =>
            {
                await Task.Delay(1000);
                this.machineService.Init();
            });
            
        }

        private void ButtonMethod(string menuName)
        {
            switch (menuName)
            {
                case "TriggerPhoto":
                    machineService.RunPhoto();
                    break;
                case "TriggerInspect":
                    machineService.RunInspect();
                    break;
                case "SwitchAutoRun":
                    machineService.SwitchAutoRun();
                    break;
                case "ManualTriggerRun":
                    machineService.ManualTriggerRun();
                    break;
                case "SaveInpsectCfg":
                    machineService.SaveInpsectCfg();
                    break;
            }
        }

        private void ReceiveMessageEvent(MessageEventArgs msg)
        {
            if (Messages.Count > 50)
            {
                Messages.RemoveAt(Messages.Count - 1);
            }
            Messages.Insert(0,msg);
        }
    }
}
